import { Module } from 'vuex'
import { State } from '..'
import { Todo } from '../../types'

const initialState = {
  todos: [] as Todo[]
}

// 导出类型：从已知中的值推断出类型
export type TodoState = typeof initialState

export default {
  // 做模块一般加上 namespaced
  namespaced: true,
  state: initialState,
  mutations: {
    initTodo (state, payload: Todo[]) {
      state.todos = payload
    },
    addTodo (state, payload: Todo) {
      state.todos.push(payload)
    }
  },
  actions: {
    // 模拟 ajax 请求
    initTodo ({ state, commit }) {
      setTimeout(() => {
        commit('initTodo', [
          {
            id: 1,
            name: 'vue3和vuex使用',
            completed: false
          }
        ])
      }, 1000)
    }
  }
} as Module<TodoState, State>
// Module<S, R> S：模板类型 R：源类型
